import {defineStore} from "pinia";
import {IActions, IAddress, IState} from "@/store/myModule/types";
import zjRequest from "@/services";
import {localCache} from "@/utils";
import router from "@/router";

export const setupMy = async () => {
    const myModule = useMyModule()
    await myModule.setupMyAction()
}
const useMyModule = defineStore<any, IState, any, IActions>("myModule", {
    state: () => ({
        userInfo: {
            id: 0,
            username: "",
            nickName: "",
            email: "",
            sign: "",
            photo: "",
            gender: 0
        },
        addressList: [],
        defaultAddress: {
            id: 0,
            receiverProvince: 0,
            receiverCity: 0,
            receiverDistrict: 0,
            receiverAddress: "",
            receiverName: "",
            receiverMobile: "",
            receiverPhone: "",
            receiverDefault: 0
        }
    }),
    actions: {
        // 获取用户信息
        async getMyAction(needNavigate = "") {
            const res = await zjRequest.get({
                url: "/sysUser/Details",
                needToken: true,
                needNavigate
            })
            if (res.code === 0) {
                return res.data
            } else {
                return null
            }
        },
        // 重置用户信息
        async resetMyAction(needNavigate) {
            localCache.setCache("token", "")
            if (needNavigate) {
                await router.push("/my/enter?return=" + needNavigate)
            }
            this.userInfo = {
                id: 0,
                username: "",
                nickName: "",
                email: "",
                sign: "",
                photo: "",
                gender: 0
            }
        },
        // 初始化用户
        async setupMyAction() {
            const data = await this.getMyAction()
            if (data) {
                this.userInfo = data
            }
        },
        // 登录
        async loginAction(params, extraConfig) {
            const res = await zjRequest.post({
                url: "/sysUser/login",
                data: params,
                ...extraConfig
            })
            if (res.code === 0) {
                const {token, userVo} = res.data
                this.userInfo = userVo
                localCache.setCache("token", token)
            }
            return res.code === 0
        },
        // 注册
        async registerAction(params) {
            const {code, ...otherParams} = params
            const res = await zjRequest.post({
                url: "/sysUser/register",
                params: {
                    code
                },
                data: otherParams,
                needToast: true
            })
            return res.code === 0;
        },
        // 退出登录
        async logoutAction() {
            const res = await zjRequest.delete({
                url: "/sysUser/logOut",
                needToken: true
            })
            if (res.code === 0) {
                this.resetMyAction("")
            }
            return res.code === 0
        },
        // 更新信息
        async updateMyAction(params) {
            const res = await zjRequest.put({
                url: "/sysUser/Details",
                data: params,
                needToken: true
            })
            return res.code === 0
        },
        // 获取收货地址
        async getMyAddressAction(params = {}) {
            const {id, needNavigate, ...otherParams} = params
            let requestUrl = "/address"
            // 有id表示获取单条
            if (id) {
                requestUrl = `/address/${id}`
            }
            const res = await zjRequest.get({
                url: requestUrl,
                params: otherParams,
                needToken: true,
                needToast: true,
                needNavigate
            })
            if (res.code === 0) {
                return res.data
            } else {
                return null
            }
        },
        // 设置收货地址
        async setAddressListAction(params) {
            const data = await this.getMyAddressAction(params)
            if (data) {
                if (Array.isArray(data)) {
                    this.addressList = data
                    this.defaultAddress = data[0]
                }
            }
            return !!data
        },
        // 添加收货地址
        async addAddressAction(params) {
            const res = await zjRequest.post({
                url: "/address",
                data: params,
                needToken: true
            })
            return res.code === 0
        },
        // 修改收货地址
        async editAddressAction(params) {
            const {needNavigate, submitForm} = params
            const res = await zjRequest.put({
                url: "/address",
                data: submitForm,
                needToken: true,
                needNavigate
            })
            return res.code === 0
        },
        // 修改默认收货地址
        async editDefaultAddressAction(params) {
            const res = await zjRequest.put({
                url: `/address/${params.addressId}`,
                needToken: true,
                needNavigate: "/my/address"
            })
            return res.code === 0
        },
        // 删除地址
        async deleteAddressAction(params) {
            const res = await zjRequest.delete({
                url: `/address/${params.addressId}`,
                needToken: true,
                needNavigate: "/my/address"
            })
            return res.code === 0
        }
    }

})
export default useMyModule
